package com.lv.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lv.mall.product.entity.ProductAttrValueEntity;
import com.lv.mall.product.service.ProductAttrValueService;
import com.lv.mall.product.vo.AttrGroupRelationVo;
import com.lv.mall.product.vo.AttrRespVo;
import com.lv.mall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lv.mall.product.entity.AttrEntity;
import com.lv.mall.product.service.AttrService;
import com.lv.common.utils.PageUtils;
import com.lv.common.utils.R;



/**
 * 商品属性
 *
 * @author lvjingyu
 * @email 2737550037@qq.com
 * @date 2021-10-12 10:42:18
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    // 查询商品的规格属性
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data",entities);
    }



    /**
     * 列表
     * 分页参数等
     * 所有参数封装为map
     * @param params
     * @return
     */
    // product/attr/sale/list/0              // 销售属性    快速显示
    // product/attr/base/list/{catelogId}   // 基本属性
//    @GetMapping("/base/list/{catelogId}")
//    public R baseAttrList(@RequestParam Map<String, Object> params,
//                          @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrService.queryBaseAttrPage(params,catelogId);
//        return R.ok().put("page", page);
//    }
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type) {  // 1、基本属性   否则销售属性
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo respVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }


    /**
     * 修改商品规格
     * @param spuId
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entities) {
        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    /**
     * 批量删除 放在了Attr_group_controller里
     * @param vos
     * @return
     */
//    @PostMapping("/attr/relation/delete")
//    public R deleteRelation(AttrGroupRelationVo[] vos) {
//        attrService.deleteRelation(vos);
//        return R.ok();
//    }



}
